#!/bin/bash
set -e
#获取输入参数
parameter="$1"

##############################################
#init参数、start参数
master_init="master"
master_start="master"

beta2_init="OpenHarmony-3.2-Beta2"
beta2_start="OpenHarmony-3.2-Beta2"

beta2_Merge_init="OpenHarmony-3.2-beta2-Merge"
beta2_Merge_start="OpenHarmony-3.2-beta2"

release_init="OpenHarmony-3.2-Release"
release_start="OpenHarmony-3.2-Release"
##############################################

#输出菜单
function print_menu(){
  echo -e "*******************************************
* Welcome to download OpenHarmony Code 	  *
* Please Choice OpenHarmony SDK:	      *
* OpenHarmony Version             	      *
*   OpenHarmony master       	prese 1.1 *
*   OpenHarmony-3.2-beta2    	press 1.2 *
*   OpenHarmony-3.2-beta2-merge press 1.3 *
*   OpenHarmony-3.2-release  	press 1.4 *
*******************************************"
read -p "please enter Version Number(example 1.1):" version_num 
  parameter=${version_num}
  check_parameter 
}

#需要用到的参数
parameter_init=""
parameter_start=""
parameter_xml=""
parameter_name=""
#查找参数
function check_parameter(){
  if [ "${parameter}" == "1.1" ];then
    parameter_init="${master_init}"
    parameter_xml="default.xml"
    parameter_start="${master_start}"
    parameter_name="${master_init}"
  elif [ "${parameter}" == "1.2" ];then
    parameter_init="${beta2_init}"
    parameter_xml="default.xml"
    parameter_start="${beta2_start}"
    parameter_name="${beta2_init}"
  elif [ "${parameter}" == "1.3" ];then
    parameter_init="${beta2_Merge_init}"
    parameter_xml="thead_develop.xml"
    parameter_start="${beta2_Merge_start}"
    parameter_name="${beta2_Merge_init}"
  elif [ "${parameter}" == "1.4" ];then
    parameter_init="${release_init}"
    parameter_xml="default.xml"
    parameter_start="${release_start}"
    parameter_name="${release_init}"
  else
    echo -e ""
    echo -e "product is not in product list, please recheck!!!"
    echo -e ""
    print_menu
    return 0
  fi
  download_path
  download
  download_info 

}

#创建并进入目录
function download_path(){
  if [ ! -e "${parameter_name}" ];then
    mkdir ${parameter_name}
    cd ${parameter_name}
  elif [ ! -e "${parameter_name}_tmp" ];then
    mkdir "${parameter_name}_tmp"
    cd "${parameter_name}_tmp"
  else
    rm "${parameter_name}_tmp" -rf
    mkdir "${parameter_name}_tmp"
    cd "${parameter_name}_tmp"
  fi
}

#下载代码
url="https://gitee.com/openharmony/manifest.git"
sync_2="repo sync -c"
lfs_3="repo forall -c 'git lfs pull'"
function download(){
  init_1="repo init -u ${url} -b ${parameter_init} -m ${parameter_xml} --no-repo-verify"
  #${sync_2}
  #${lfs_3}
  set_br_4="repo start  ${parameter_start} --all"
  echo "*******************************************"
}

#下载信息
function download_info(){
  echo -e "you have been download OpenHarmony ${parameter_name} code
url       :${url}
branch    :${parameter_name}
xml_name  :${parameter_xml}
code_path :$(pwd)
init   cmd:${init_1}
sync   cmd:${sync_2}
lfs    cmd:${lfs_3}
set_br cmd:${set_br_4}"
}

function main(){
  clear
  if [ ! "${parameter}" ];then
    print_menu
  else
    check_parameter
  fi
}

main 
